<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class GiftWeekRepository {

    /**
     * 获取礼物列表
     * @param $where
     * @param $where_gift
     * @param $search_time
     * @param $page
     * @param $perPage
     * @return bool
     */
    public function giftWeekList($where_gift,$where,$search_time,$page = 1, $perPage = 10) {
        
        $giftWeekList = app('model_gift_week')
            ->select('id', 'gift_id_1', 'gift_id_2', 'create_time', DB::raw('if(YEARWEEK(NOW(),1) < weeks,1,0) as is_delete'), DB::raw('if(weeks=0,"",concat_ws("-",left(weeks,4),right(weeks,2))) as date'))
            ->with('gift_one')
            ->with('gift_two')
            ->whereBetween('create_time',$search_time)
            ->where($where_gift)
            ->where(function($query) use ($where) {
                $query->whereHas('gift_one', function($query) use ($where) {
                        $query->where($where);
                    })->orWhereHas('gift_two', function($query) use ($where) {
                    $query->where($where);
                });
            })
            ->paginate($perPage, ['*'], '', $page);
        return $giftWeekList;
    }

    /**
     * 根据年份和周数 获取日期
     * @param $year
     * @param $weeknum
     * @return array
     */
    public function getWeekDate($year, $weeknum) {
        $firstdayofyear = mktime(0, 0, 0, 1, 1, $year);
        $firstweekday = date('N', $firstdayofyear);
        $firstweenum = date('W', $firstdayofyear);
        if ($firstweenum == 1) {
            $day = (1 - ($firstweekday - 1)) + 7 * ($weeknum - 1);
            $startdate = date('Y-m-d', mktime(0, 0, 0, 1, $day, $year));
            $enddate = date('Y-m-d', mktime(0, 0, 0, 1, $day + 6, $year));
        } else {
            $day = (9 - $firstweekday) + 7 * ($weeknum - 1);
            $startdate = date('Y-m-d', mktime(0, 0, 0, 1, $day, $year));
            $enddate = date('Y-m-d', mktime(0, 0, 0, 1, $day + 6, $year));
        }
        return array($startdate, $enddate);
    }

    /*
     * 新增周星礼物
     * @param $param
     * @return bool
     * @throws BasicException
     *
     * */
    public function addWeekGift($param) {
        $addWeekGift = app('model_gift_week')->create($param);
        if ($addWeekGift) return $addWeekGift;
        throw new BasicException(93028);
    }

    /*
     * 编辑周星礼物
     * @param $param
     * @return bool
     * @throws BasicException
     * */
    public function saveWeekGift($param) {
        $saveWeekGift = app('model_gift_week')->where('id', $param['id'])->update($param);
        if ($saveWeekGift) return $saveWeekGift;
        throw new BasicException(93029);
    }

    /*
     * 删除礼物
     * @param $param
     * @return bool
     * @throws BasicException
     * */
    public function delWeekGift($param) {
        $addGift = app('model_gift_week')->whereIn('id', $param)->delete();
        if ($addGift) return $addGift;
        throw new BasicException(93030);
    }


    /*
     * 周星礼物详情
     * @param $id
     * @return bool
     * @throws BasicException
     *
     * */
    public function giftWeekDetail($id) {

        $giftWeekDetail = app('model_gift_week')
            ->select('id', 'gift_id_1', 'gift_id_2', 'create_time', DB::raw('if(YEARWEEK(NOW(),1) < weeks,1,0) as is_delete'), DB::raw('if(weeks=0,"",concat_ws("-",left(weeks,4),right(weeks,2))) as date'))
            ->with('gift_one')
            ->with('gift_two')
            ->where('id',$id)
            ->first();
        if ($giftWeekDetail) return $giftWeekDetail;
        throw new BasicException(93031);
    }

    /*
     * 获取奖品列表：用于编辑 新增 搜索
     * @return bool
     * @throws BasicException
     * */
    public function  giftList(){
        $giftWeekDetail = app('model_gift')->where(['type'=>1,'sold'=>1])->select('id','name')->get()->toArray();
        return $giftWeekDetail;
    }
}


?>